package com.zhong.beautifulimageapp;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.NavigationMenuView;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.zhong.beautifulimageapp.adapter.CommonFragmentAdapter;
import com.zhong.beautifulimageapp.fragments.AllTopicFragment;
import com.zhong.beautifulimageapp.fragments.AskTopicFragment;
import com.zhong.beautifulimageapp.fragments.GoodTopicFragment;
import com.zhong.beautifulimageapp.fragments.JobTopicFragment;
import com.zhong.beautifulimageapp.fragments.ShareTopicFragment;
import com.zhong.beautifulimageapp.model.UserAfterLoginInfo;
import com.zhong.beautifulimageapp.utils.BaseFragments;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

    @BindView(R.id.main_toolbar_getLeft)
    ImageView mGetMenuLeft;
    @BindView(R.id.main_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.main_tab_item)
    TabLayout mTabItem;
    @BindView(R.id.main_view_pager)
    ViewPager mViewPager;
    @BindView(R.id.main_navigation_view)
    NavigationView mNavigationView;
    @BindView(R.id.main_drawerLayout)
    DrawerLayout mDrawerLayout;
    private UserAfterLoginInfo mUserAfterLoginInfo;
    private TextView mOut;
    private TextView mLoginName;
    private ImageView mUserIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }
        View headerView = mNavigationView.getHeaderView(0);
        mOut = (TextView) headerView.findViewById(R.id.nav_header_loginOrOut);
        mOut.setOnClickListener(this);
        mUserAfterLoginInfo = new UserAfterLoginInfo();
        mLoginName = ((TextView) headerView.findViewById(R.id.user_name));
        mUserIcon = (ImageView) headerView.findViewById(R.id.user_icon);
        setHeaderMessage();
        headerView.findViewById(R.id.nav_header_login).setOnClickListener(this);
        setNavigationViewScrollBarGone(mNavigationView);
        mNavigationView.setNavigationItemSelectedListener(this);
        List<BaseFragments> baseFragmentsList = new ArrayList<>();
        AllTopicFragment allTopicFragment = new AllTopicFragment();
        baseFragmentsList.add(allTopicFragment);
        GoodTopicFragment goodTopicFragment = new GoodTopicFragment();
        baseFragmentsList.add(goodTopicFragment);
        ShareTopicFragment shareTopicFragment = new ShareTopicFragment();
        baseFragmentsList.add(shareTopicFragment);
        AskTopicFragment askTopicFragment = new AskTopicFragment();
        baseFragmentsList.add(askTopicFragment);
        JobTopicFragment jobTopicFragment = new JobTopicFragment();
        baseFragmentsList.add(jobTopicFragment);
        CommonFragmentAdapter adapter = new CommonFragmentAdapter(getSupportFragmentManager(), baseFragmentsList);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(4);
        mTabItem.setupWithViewPager(mViewPager);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setHeaderMessage();
    }

    private void setHeaderMessage() {
        getTheUserAfterLoginInfo(mUserAfterLoginInfo);
        if (!mUserAfterLoginInfo.getLoginName().equals("")) {
            mOut.setVisibility(View.VISIBLE);
            mLoginName.setText(mUserAfterLoginInfo.getLoginName());
            Glide.with(this)
                    .load(mUserAfterLoginInfo.getUserIcon())
                    .asBitmap()
                    .placeholder(R.mipmap.app)
                    .centerCrop()
                    .into(new BitmapImageViewTarget(mUserIcon){
                        @Override
                        protected void setResource(Bitmap resource) {
                            RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), resource);
                            drawable.setCircular(true);
                            mUserIcon.setImageDrawable(drawable);
                        }
                    });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_toolbar_more, menu);
        return true;
    }

    private void setNavigationViewScrollBarGone(NavigationView navigationView){
        NavigationMenuView menuView = (NavigationMenuView) navigationView.getChildAt(0);
        if (menuView != null) {
            menuView.setVerticalScrollBarEnabled(false);
        }
    }

    @OnClick({R.id.main_toolbar_getLeft, R.id.main_toolbar_app_icon_getLeft})
    public void showTheMenu() {
        mDrawerLayout.openDrawer(GravityCompat.START, true);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            default:
                break;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.nav_header_login:
                if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                    mDrawerLayout.closeDrawer(GravityCompat.START);
                }
                Intent intent;
                if (!mUserAfterLoginInfo.getLoginName().equals("")){
                    intent = new Intent(this, MyActivity.class);
                }else {
                    intent = new Intent(this, LoginActivity.class);
                }
                startActivity(intent);
                break;
            case R.id.nav_header_loginOrOut:
                AlertDialog dialog = new AlertDialog.Builder(this)
                        .setMessage(R.string.Out_AltDialog_message)
                        .setNegativeButton(R.string.mainActivity_out_AltDialog_NO, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setPositiveButton(R.string.mainActivity_out_AltDialog_YES, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                clearTheUserAfterLoginInfo();
                                getTheUserAfterLoginInfo(mUserAfterLoginInfo);
                                mOut.setVisibility(View.INVISIBLE);
                                mLoginName.setText(R.string.click_to_login);
                                mUserIcon.setImageResource(R.mipmap.app);
                            }
                        })
                        .create();
                dialog.show();
                break;
            default:
                break;
        }
    }
}
